package com.dw.etl.core.springbatch.writer;

import com.dw.etl.common.base.mapper.BaseMapper;
import com.dw.etl.core.db.RoutingDataSource;
import com.dw.etl.core.db.ThreadLocalDataSource;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.item.ItemWriter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * 根据指标查询最小维度dm表，
 * 如果表不存在就重新创建，如果属性不匹配（无论是多了还是少了），需重删除原表重抽数据
 * 执行插入
 * Created by liujunlei on 2021/5/25.
 */
@Component
@StepScope
public class DMInsertWriter implements ItemWriter {

    @Value("#{jobParameters['tableName']}")
    private String tableName;

    @Value("#{jobParameters['idDbTar']}")
    private String idDbTar;

    @Autowired
    BaseMapper baseMapper;

    @Override
    @RoutingDataSource
    public void write(List list) throws Exception {
        ThreadLocalDataSource.setLocalSource(idDbTar);
        List<Map<String,Object>> mapList = (List<Map<String,Object>>)list;
        baseMapper.insertMapList(tableName,mapList.get(0),mapList);
    }
}
